package com.meitianhui.convenitenbuy.module.home.ui.activity.web;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseWebViewActivity;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.hyphenate.ui.ChatActivity;
import com.meitianhui.convenitenbuy.module.home.ui.activity.JoinFightGroupActivity;
import com.meitianhui.convenitenbuy.module.home.ui.activity.OpenFightGroupActivity;
import com.meitianhui.convenitenbuy.module.store.ui.activity.GroupShopCarActivity;
import com.meitianhui.convenitenbuy.util.JumpUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by Administrator on 2017/11/14.
 * 拼团领
 */

public class FightGroupWebViewActivity extends BaseWebViewActivity{
    private HashMap<String, String> mTitleMap = new HashMap<>();// 存放url和上一个界面的标题
    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        if (!TextUtils.isEmpty(mTitle))
        setTbMenu(R.mipmap.icon_title_share);
        mCurrentName = "拼团领";
        mWebView.addJavascriptInterface(new MyJavaScriptInterface(this), "jsObj");//js调用安卓
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
    }

    @Override
    protected void urlLoading(WebView view, String url) {
        if (url.contains("detail")||url.contains("publish")||url.contains("help")) {
            Intent intent = new Intent(FightGroupWebViewActivity.this, FightGroupWebViewActivity.class);
            intent.putExtra("url", url);
            if("help".contains(url)) {
                intent.putExtra("title", "帮助");
            }else{
                intent.putExtra("title", "商品详情");
            }
            intent.putExtra("type","group-purchase");
            intent.putExtra("is_second",true);
            startActivity(intent);
        }else {
            view.loadUrl(url);
        }
    }
    @Override
    protected void clickMenu() {
        super.clickMenu();
        share(Constants.TYPE_GOOD_DETAIL_PAGE);

    }
    @Override
    protected void receivedTitle(WebView view, String title) {
        if (!mWebView.canGoBack()) {// 加载首页
            setTitle(title);
        } else {
            mTitleMap.put(view.getUrl(), mTb_title.getText().toString());
        }
        mTb_title.setText(title);
    }

    @Override
    protected void onRefreshData() {

    }

    @Override
    protected void isFinish() {
        if (mWebView.canGoBack()) {
            String currentUrl = mWebView.getUrl();
            if (mTitleMap.isEmpty() || TextUtils.isEmpty(mTitleMap.get(currentUrl))) {
            } else {
                mTitleMap.remove(currentUrl);
            }
            mWebView.goBack();
        } else {
            finish();
            mTitleMap.clear();
        }
    }

    @Override
    protected void shareWxMini(View v) {

    }

    //js调用安卓代码
    private class MyJavaScriptInterface {
        private Handler handler = new Handler();
        private Context mContext;

        public MyJavaScriptInterface(Context c) {
            this.mContext = c;
        }

        @JavascriptInterface
        public void  createHptGroup (final String json){   //开团
            if(!TextUtils.isEmpty(json)){
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            JSONObject jsonObject=new JSONObject(json);
                            String goodsId=jsonObject.getString("goods_id");
                            Intent intent = new Intent(FightGroupWebViewActivity.this, OpenFightGroupActivity.class);
                            intent.putExtra("goods_id", goodsId);
                            intent.putExtra("json", json);
                            startActivity(intent);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, 0);
            }
        }
        //伙拼团跳购物车
        @JavascriptInterface
        public void buyGroupGoods(final String invitationCode) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(FightGroupWebViewActivity.this, GroupShopCarActivity.class);
                    intent.putExtra("invitation_code", invitationCode);
                    startActivity(intent);
                }
            }, 0);
        }
        @JavascriptInterface
        public void  joinHptGroup  (final String json){   //参团
            if(!TextUtils.isEmpty(json)){
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            JSONObject jsonObject=new JSONObject(json);
                            String activityId=jsonObject.getString("activity_id");
                            Intent intent = new Intent(FightGroupWebViewActivity.this, JoinFightGroupActivity.class);
                            intent.putExtra("activity_id", activityId);
                            intent.putExtra("json", json);
                            startActivity(intent);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, 0);
            }
        }
        //打开广告页
        @JavascriptInterface
        public void openAdLayer(final String jsonData) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Log.v("zyl", "openAdLayer jsonData:" + jsonData);
                    try {
                        JSONObject jsonObject = new JSONObject(jsonData);
                        String url = jsonObject.getString("url");
                        Intent intent = new Intent(FightGroupWebViewActivity.this, AdWebViewActivity.class);
                        intent.putExtra("url", url);
                        startActivity(intent);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            });
        }
        //客服功能
        @JavascriptInterface
        public void chatWithCustomerServiceAgent(final  String chat){
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        if(!InfoProvider.getLoginStatus(FightGroupWebViewActivity.this)){
                            JumpUtil.jumpToLoginActivity(0, FightGroupWebViewActivity.this, 0, "");
                            return;
                        }
                        JSONObject jsonObject = new JSONObject(chat);
                        String chatid = jsonObject.getString("chat_id");
                        String title = jsonObject.getString("title");
                        String hand=jsonObject.getString("head_pic_path");
                        Intent intent = new Intent(FightGroupWebViewActivity.this, ChatActivity.class);
                        intent.putExtra("userId", chatid);
                        intent.putExtra("groupName", title);
                        startActivity(intent);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, 0);
        }
        //分享功能
        @JavascriptInterface
        public void  share(String s_1){
            if(!TextUtils.isEmpty(s_1)){
                try {
                    JSONObject jsonObject=new JSONObject(s_1);
                    mShareTitle = jsonObject.getString("title");
                    mShareDesc = jsonObject.getString("desc");
                    mShareImgPath = jsonObject.getString("imgPath");
                    mShareUrl = jsonObject.getString("url");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mWebView.canGoBack()) {
            String currentUrl = mWebView.getUrl();{
                mTitleMap.remove(currentUrl);
            }
            if (mTitleMap.isEmpty() || TextUtils.isEmpty(mTitleMap.get(currentUrl))) {
            } else
            mWebView.goBack();
        } else {
            finish();
            mTitleMap.clear();
        }
        return super.onKeyDown(keyCode, event);
    }

}
